package work.dp;

/**
 * @author WDH
 * @description
 * @date 2024年11月30日 14:59
 */

public class Problem53 {
    public int maxSubArray(int[] nums) {
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        int sum = dp[0];
        for (int i = 1; i < nums.length; i++) {
            dp[i] = dp[i - 1] + nums[i] > nums[i] ? dp[i - 1] + nums[i] : nums[i];
            sum=Math.max(dp[i], sum);
        }
        return sum;
    }

    public static void main(String[] args) {
        int[] dp = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
        Problem53 problem53 = new Problem53();
        problem53.maxSubArray(dp);
    }
}
